﻿using Nova_BLL;
using Nova_Common.Extend;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.RFSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.RFSystem.Controllers
{
    /// <summary>
    /// 补货操作
    /// </summary>
    [RoutePrefix("api/RFSystem/ReplenStorage")]
    [ControllerGroup("PDA手持", "补货操作功能接口")]
    public class ReplenStorageController : BaseApiController
    {
        RFBll RfBll = new RFBll();

        /// <summary>
        /// 根据托盘码获取指令,物料等出库信息
        /// </summary>
        /// <param name="trayId">托盘码</param>
        /// <returns></returns>
        [HttpGet, Route("GetReplenCommandMatInfoByTrayId")]
        public ApiResult GetReplenCommandMatInfoByTrayId(string trayId)
        {
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘码不能为空!");
            DataTable dt = RfBll.GetReplenCommandMatInfoByTrayId(trayId);
            List<TrayReplenStorageCommandModel> list = Common.DataTableToList<TrayReplenStorageCommandModel>(dt);
            var model = list?.FirstOrDefault();
            if (model != null)
            {
                decimal stockNum = RfBll.GetTrayMatNumBySectId(model.Tray_ID, model.Sec_Mat_ID);
                model.StockNum = stockNum;
            }
            return Success("根据托盘码获取补货指令,物料等出库信息!", model);
        }

        /// <summary>
        /// 补货
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ApiResult ReplenStorage(SetPackageModel param)
        {
            StoreProceResult res = RfBll.ReplenPackage(param.TaskId, param.StrStockNo, param.StrTrayId, param.MaterialCode, param.SecMatId, param.MatNum, param.Size);
            if (res.ResNo == 1)
                return Success("补货成功!");
            else
                return Error(ResultCode.Error, res.ResMsg);
        }

        /// <summary>
        /// 根据托盘码获取补货指令信息
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        [HttpGet, Route("GetRepCommandByTrayId")]
        public ApiResult GetRepCommandByTrayId(string trayId)
        {
            TCommandBll cmdBll = new TCommandBll();
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘码不能为空!");
            var cmdEntity = cmdBll.GetByTrayId(trayId);
            if (cmdEntity != null && cmdEntity.TaskMode == 5)
                return Error("此托盘当前存在补货指令，请先过账完成补货指令!");
            int rows = RfBll.GetRelepenCmdRecordByTrayId(trayId);
            return Success("根据托盘码获取出库指令信息", rows);
        }

        /// <summary>
        /// 获取托盘补货数据
        /// </summary>
        /// <param name="trayId"></param>
        /// <returns></returns>
        [HttpGet, Route("GetReplenCommandRecordByTrayId")]
        public ApiResult GetReplenCommandRecordByTrayId(string trayId)
        {
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘码不能为空!");
            DataTable dt = RfBll.GetReplenCommandRecordByTrayId(trayId);
            List<TrayReplenCommandRecordModel> list = Common.DataTableToList<TrayReplenCommandRecordModel>(dt);
            var model = list?.FirstOrDefault();
            return Success("根据托盘补货修改记录数据!", model);
        }

        /// <summary>
        /// 补货超重
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ApiResult ReplenStorageOverWeight(SetPackageModel param)
        {
            StoreProceResult res = RfBll.ReplenStorageOverWeight(param.TaskId, param.StrStockNo, param.StrTrayId, param.SecMatId, param.MatNum);
            if (res.ResNo == 1)
                return Success("补货超重数量修改成功!");
            else
                return Error(ResultCode.Error, res.ResMsg);
        }
    }
}
